package com.ffcc.fitness.merchant.controller;

import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.merchant.pojo.dto.MerchantUpdateParam;
import com.ffcc.fitness.merchant.pojo.entity.Merchant;
import com.ffcc.fitness.merchant.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("v1/merchant/")
@Slf4j
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    @PostMapping("update")
    public JsonResult update(MerchantUpdateParam merchantUpdateParam){
        Merchant merchant = merchantService.update(merchantUpdateParam);
        return JsonResult.ok(merchant);
    }

    @GetMapping("selectByMerchantId")
    public JsonResult selectByMerchantId(@RequestParam Long merchantId) {
        log.info("根据商家ID查询商家信息: merchantId={}", merchantId);

        try {
            // 查询商家信息
            Merchant merchant = merchantService.selectByMerchantId(merchantId);

            if (merchant == null) {
                return new JsonResult(5005, "商家不存在");
            }

            // 如果商家没有QR码，动态生成一个
            if (merchant.getQrCode() == null || merchant.getQrCode().trim().isEmpty()) {
                log.info("商家没有QR码，开始生成: merchantId={}", merchantId);
                String qrCodeUrl = merchantService.generateQRCodeForMerchant(merchantId);
                merchant.setQrCode(qrCodeUrl);
            }

            return JsonResult.ok(merchant);

        } catch (Exception e) {
            log.error("查询商家信息失败: merchantId={}", merchantId, e);
            return new JsonResult(5001, "查询失败: " + e.getMessage());
        }
    }

    @GetMapping("selectByUserId")
    public JsonResult selectByUserId(@RequestParam Long userId) {
        log.info("根据用户ID查询商家信息: userId={}", userId);

        try {
            // 查询商家信息
            Merchant merchant = merchantService.selectByUserId(userId);

            if (merchant == null) {
                return new JsonResult(5005, "用户ID对应的商家不存在");
            }

            // 如果商家没有QR码，动态生成一个
            if (merchant.getQrCode() == null || merchant.getQrCode().trim().isEmpty()) {
                log.info("商家没有QR码，开始生成: userId={}, merchantId={}", userId, merchant.getMerchantId());
                String qrCodeUrl = merchantService.generateQRCodeForUser(userId);
                merchant.setQrCode(qrCodeUrl);
            }

            return JsonResult.ok(merchant);        } catch (Exception e) {
            log.error("根据用户ID查询商家信息失败: userId={}", userId, e);
            return new JsonResult(5001, "查询失败: " + e.getMessage());
        }
    }

    @GetMapping("generateOrderQRCode")
    public JsonResult generateOrderQRCode(@RequestParam Long userId,
                                         @RequestParam String equipmentName,
                                         @RequestParam Integer quantity,
                                         @RequestParam String totalAmount) {
        log.info("为订单生成动态QR码: userId={}, equipmentName={}, quantity={}, totalAmount={}", 
                userId, equipmentName, quantity, totalAmount);

        try {
            String qrCodeUrl = merchantService.generateOrderQRCodeForUser(userId, equipmentName, quantity, totalAmount);
            return JsonResult.ok(qrCodeUrl);

        } catch (Exception e) {
            log.error("生成订单QR码异常", e);
            return new JsonResult(5001, "生成订单QR码失败");
        }
    }
}
